<?php
require_once "include/openid.php";
require_once "db/db.php";
function LoginGoogle()
	{
		try {
		    $openid = new LightOpenID;
		    if(!$openid->mode) {
		        
					//link yahoo's openid 
		            $openid->identity = 'https://www.google.com/accounts/o8/id';
		             //request information 
		            $openid->required = array('contact/email');
		            header('Location: ' . $openid->authUrl());
		
		    } elseif($openid->mode == 'cancel') {
		        echo 'User has canceled authentication!';
		    } else {
		      $login = $openid->validate();
		      if($login)
		         {
		         	//get email infor from service
			  		 	$infor = $openid->getAttributes();		    
		    			global $email;
						$email = $infor['contact/email'];						
						
						//query find email in database
						$select_email = "SELECT * FROM `oUsers` WHERE `email` = '$email'";
						$result_select_email = DbSelect($select_email);
						//check user in database
						if($result_select_email)
							{
								//construct session in new one active
								foreach($result_select_email as $values)
									{
										$_SESSION['user_id'] = $values['id'];
										$_SESSION['full_name'] = $values['fullName'];
										
										//image defaulf 
										if($values['image'] == "")
											{
												//is femail
												if($values['gender'] == 0)
													$_SESSION['image'] = "IB7NOFmPw2a.gif";
												//is mail
												else
													$_SESSION['image'] = "UlIqmHJn-SK.gif";
											}
										//image user uploaded	
										else
											$_SESSION['image'] = $values['image'];	
									}
								echo "<p class = 'complete'>Đăng nhập thành công. Quay về  
								<a href =  'index.php'>trang chủ</a>.";
								echo "<meta http-equiv=\"refresh\" content=\"5;URL=index.php\">";
							}
						
						//create new user 
						else
						 {						 	
							echo "<p class = 'complete'>Tài khoản mới. OseeYou sẽ giúp bạn đăng kí với 
							tài khoản email <b>$email</b>.</p>";
							include "pg/singup-openid.php";														
							
						 }//end else
		         }//end if
		      else
		         echo  "<p class = 'error'>Đăng nhập thất bại.</p>";

		    }
	
		   
		    
		} catch(ErrorException $e) {
		    echo $e->getMessage();
		}
	}
?>